package com.saya.model;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.saya.utils.Utils;

public class Product {
	private String name;
	  private String description;

	  public String getName()
	  {
	    return this.name;
	  }

	  public void setName(String name) {
	    this.name = name;
	  }

	  public String getDescription() {
	    return this.description;
	  }

	  public void setDescription(String description) {
	    this.description = description;
	  }

	  public static Boolean createOrUpdateProduct(String name, String description) {
	    Entity product = Utils.getEntity("Product", name);
	    if (product == null) {
	      product = new Entity("Product", name);
	      product.setProperty("Description", description);
	    } else {
	      product.setProperty("Description", description);
	    }
	    Key k = Utils.persistEntity(product);
	    if ((k != null) && (k.getName().equals(name))) {
	      return Boolean.valueOf(true);
	    }
	    return Boolean.valueOf(false);
	  }

	  public static Product getProduct(String name)
	  {
	    Entity product = Utils.getEntity("Product", name);
	    if (product == null) {
	      return null;
	    }
	    Product pro = new Product();
	    pro.setName(name);
	    pro.setDescription((String)product.getProperty("Description"));
	    return pro;
	  }

	  public static List<Product> getAllProduct()
	  {
	    Iterable<Entity> lstEntity = Utils.listEntities("Product", null, null);
	    List lstProduct = new ArrayList();
	    for (Entity entity : lstEntity) {
	      Product pro = new Product();
	      pro.setName(entity.getKey().getName());
	      pro.setDescription((String)entity.getProperty("Description"));
	      lstProduct.add(pro);
	    }
	    return lstProduct;
	  }
}
